package controller;

import domain.Teacher;
import service.TeacherService;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();
    public void Teacherstart() {
        teacherLoop:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                   // System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    delete();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    update();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    find();
                    break;
                case "5":
                    System.out.println("感谢您使用老师管理系统, 再见!");
                    break teacherLoop;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }
    }

    public void update() {
        String sid = getString();
        Teacher teacher = getTeacher(sid);
        teacherService.update(sid,teacher);
        System.out.println("修改成功");
    }

    private void delete() {
        String sid = getString();
        teacherService.delete(sid);
        System.out.println("删除成功");
    }

    private String getString(){
        String sid=null;
        while (true){
            System.out.println("请输入工号");
            sid=sc.next();
            boolean flag = teacherService.isExists(sid);
            if (flag){
                break;
            }else {
                System.out.println("不存在，请重新输入");
            }
        }
        return sid;
}

    private void find() {
       ArrayList<Teacher>list= teacherService.find();
        System.out.println(list);
    }

    public void addTeacher() {
        String sid = null;
        while (true) {
            System.out.println("请输入工号");
            sid = sc.next();
            boolean flag =teacherService.isExists(sid);
            if (flag) {
                System.out.println("已经存在，请重新输入");
            } else {
                break;
            }
            Teacher teacher = getTeacher(sid);
        teacherService.addteacher(teacher);
            System.out.println("添加成功");
        }
    }
    private Teacher getTeacher(String sid){
        System.out.println("请输入名字");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();

        Teacher teacher = new Teacher(sid,name,age,birthday);
        return teacher;
    }
}
